package hk.ust.cse.comp3111.studentSchedule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ImportByCourse_1 extends Fragment {
	
	// List view
    private ListView lv;
     
    // Listview Adapter
    ArrayAdapter<String> adapter;
     
    // Search EditText
    EditText inputSearch;
     
    // ArrayList for Listview
    ArrayList<HashMap<String, String>> courseList;
    
    CourseDatabaseHandler db;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.import_by_course_1, container, false);
         
        return rootView;
    }
 
    @Override
    public void onStart() {
    	super.onStart();
    	
    	db=new CourseDatabaseHandler(this.getActivity());
    	List<String> courseCode = db.getAllCourseCode();
    	String courseCodeArray[] = courseCode.toArray(new String[courseCode.size()]);

        lv = (ListView) this.getView().findViewById(R.id.list_view);
        inputSearch = (EditText) this.getView().findViewById(R.id.inputSearch);
         
        // Adding items to listview
        adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, R.id.course_code, courseCodeArray);
        lv.setAdapter(adapter);
        
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            	push(lv.getItemAtPosition(position).toString());
            }

			
        });
         
        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {
             
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                adapter.getFilter().filter(cs);   
            }
             
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }


        });
    }
    
    private void push(String item) {
    	Bundle save=new Bundle();
		save.putString("SELECTED",item);
		Fragment fragment = new ImportByCourse_2();
		fragment.setArguments(save); 
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.frame_container, fragment).commit();
		
	}


}
